package gw.com.android.ui.views;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import gw.com.android.ui.InspectNetworkActivity;
import gw.com.android.utils.PackStatis;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import www.com.library.util.DeviceUtil;
import www.com.library.util.NetworkMonitor;

/**
 * @author jett
 * @since 2017-12-04.
 */
public class NetworkExceptionView extends LinearLayout {

    private CompositeDisposable disposable;

    public NetworkExceptionView(Context context) {
        super(context);
        init();
    }

    public NetworkExceptionView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NetworkExceptionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        setBackgroundColor(0xFFFEE7E7);

        ImageView iv = new ImageView(getContext());
        iv.setImageResource(R.mipmap.a_tooltip_caution1);
        LayoutParams params = new LayoutParams(DeviceUtil.instance().dip2px(16, getContext()), DeviceUtil.instance().dip2px(16, getContext()));
        params.leftMargin = DeviceUtil.instance().dip2px(12, getContext());
        params.rightMargin = DeviceUtil.instance().dip2px(8, getContext());
        addView(iv, params);

        TextView tv = new TextView(getContext());
        tv.setText("网络异常！请检查您的网络设置");
        tv.setTextSize(14);
        tv.setTextColor(0xFFDD384C);
        addView(tv);

        setVisibility(NetworkMonitor.isNetworkReady(getContext()) ? GONE : VISIBLE);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getContext().startActivity(new Intent(getContext(), InspectNetworkActivity.class));
            }
        });

        Disposable netRegister = RxBus.getInstance().register(GTSConst.REPLY_NETWORK_STATE + "", Boolean.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean isNetWork) throws Exception {
                        onNetwork(isNetWork);
                    }
                });

        disposable = new CompositeDisposable();
        disposable.add(netRegister);
    }

    private void onNetwork(boolean hasNetwork) {
        setVisibility(hasNetwork ? GONE : VISIBLE);
        if (!hasNetwork) {
            PackStatis.getToService("clickabnormal", PackStatis.EventCategory.MAIN.getValue(), null, null);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (disposable != null) {
            disposable.clear();
        }
    }
}
